{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object foo at 0x7f732414e0c0>\n",
      "starting*****\n",
      "4\n",
      "********************\n",
      "res: None\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "def foo():\n",
    "    print(\"starting*****\")\n",
    "    while True:\n",
    "        res = yield 4\n",
    "        print(\"res:\",res)\n",
    "\n",
    "g = foo()\n",
    "print(g)\n",
    "print(next(g))\n",
    "print(\"*\" * 20)\n",
    "print(next(g))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20."
     ]
    }
   ],
   "source": [
    "def foo():\n",
    "    res = 1\n",
    "    while True:\n",
    "        yield res\n",
    "        res += 1\n",
    "\n",
    "g = foo()\n",
    "for i in g:\n",
    "    print(i, end=\".\")\n",
    "    if i == 20:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object foo at 0x7f7302141630>\n",
      "starting*****\n",
      "4\n",
      "********************\n",
      "res: None\n",
      "4\n",
      "res: 12\n",
      "res: end\n",
      "end\n"
     ]
    },
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[9], line 18\u001b[0m\n\u001b[1;32m     16\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(g))\n\u001b[1;32m     17\u001b[0m g\u001b[38;5;241m.\u001b[39msend(\u001b[38;5;241m12\u001b[39m)\n\u001b[0;32m---> 18\u001b[0m g\u001b[38;5;241m.\u001b[39msend(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mend\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# send\n",
    "def foo():\n",
    "    print(\"starting*****\")\n",
    "    while True:\n",
    "        res = yield 4\n",
    "        print(\"res:\", res)\n",
    "        if res == \"end\":\n",
    "            print(\"end\")\n",
    "            break\n",
    "\n",
    "\n",
    "g = foo()\n",
    "print(g)\n",
    "print(next(g))\n",
    "print(\"*\" * 20)\n",
    "print(next(g))\n",
    "g.send(12)\n",
    "g.send(\"end\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "def proc(c):\n",
    "    print(\"生产开始\")\n",
    "    for i in range(1, 11):\n",
    "        print(f\"生产产品: {i}\")\n",
    "        c.send(str(i))\n",
    "        time.sleep(1)\n",
    "\n",
    "\n",
    "def com():\n",
    "    while True:\n",
    "        res = yield\n",
    "        print(f\"消费产品: {res}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    c = com()\n",
    "    c.__next__()\n",
    "    proc(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "#生产者\n",
    "def produce(c):\n",
    "\tc.send(None)\n",
    "\tfor i in range(1,6):\n",
    "\t\tprint('生产者生产%d 产品'%i)\n",
    "\t\tc.send(str(i))\n",
    "\t\ttime.sleep(1)\n",
    "#消费者\n",
    "def customer():\n",
    "\tres=''\n",
    "\twhile True:\n",
    "\t\tdata = yield res\n",
    "\t\tif not data:\n",
    "\t\t\treturn\n",
    "\t\tprint('消费者消费%s 产品'%data)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    c = customer()\n",
    "    produce(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'generator'>\n",
      "<class 'list'>\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 生成器表达式\n",
    "my_generator = (x for x in range(10))\n",
    "print(type(my_generator))\n",
    "\n",
    "my_list = [x for x in range(11, 20)]\n",
    "print(type(my_list))\n",
    "\n",
    "list1 = list(my_generator)\n",
    "print(list1)\n",
    "# 遍历生成器\n",
    "for item in my_generator:\n",
    "    print(item, end=\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio, time\n",
    "\n",
    "\n",
    "async def work(m):\n",
    "    print(f\"work: {m}\")\n",
    "    # await asyncio.sleep(2)\n",
    "\n",
    "loop = asyncio.get_event_loop()\n",
    "tasks = [loop.create_task(work(n)) for n in [200, 400, 600]]\n",
    "print(tasks)\n",
    "loop.run_until_complete(asyncio.gather(*tasks))\n",
    "print(tasks)\n",
    "\n",
    "loop.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "\n",
    "async def my_task():\n",
    "    await asyncio.sleep(1)\n",
    "    return \"Task completed\"\n",
    "\n",
    "\n",
    "async def main():\n",
    "    task = asyncio.create_task(my_task())\n",
    "    await asyncio.sleep(0.5)  # 模拟等待一段时间\n",
    "    print(f\"Task state: {task.state}\")  # 可能显示 PENDING 或 RUNNING\n",
    "    await task\n",
    "    print(f\"Task state: {task.state}\")  # 显示 DONE\n",
    "    print(f\"Task result: {task.result()}\")\n",
    "\n",
    "\n",
    "asyncio.run(main())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "async def main():\n",
    "    print(\"Hello\")\n",
    "    await asyncio.sleep(1)\n",
    "    print(\"World\")\n",
    "\n",
    "asyncio.run(main())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
